<?php
/// ------------
/// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
/// ------------
/// | Copyright (c) 2006-2012 http:///thinkphp.cn All rights reserved.
/// ------------
/// | Licensed ( http:///www.apache.org/licenses/LICENSE-2.0 )
/// ------------
/// | Author: liu21st <liu21st@gmail.com>
/// ------------
/// $Id: View.class.php 2954 2012-05-21 03:32:24Z liu21st@gmail.com $

/**
 * ThinkPHP 视图输出
 * @category   Think
 * @package  Think
 * @subpackage  Core
 * @author liu21st <liu21st@gmail.com>
 * @version  $Id: View.class.php 2954 2012-05-21 03:32:24Z liu21st@gmail.com $
 */
class View {
    protected $tVar        =  array(); /// 模板输出变量

    /**
     * 模板变量赋值
     * @access public
     * @param mixed $name
     * @param mixed $value
     */
    public function assign($name,$value=''){
        if(is_array($name)) {
            $this->tVar   =  array_merge($this->tVar,$name);
        }elseif(is_object($name)){
            foreach($name as $key =>$val)
                $this->tVar[$key] = $val;
        }else {
            $this->tVar[$name] = $value;
        }
    }

    /**
     * 取得模板变量的值
     * @access public
     * @param string $name
     * @return mixed
     */
    public function get($name){
        if(isset($this->tVar[$name]))
            return $this->tVar[$name];
        else
            return false;
    }

    /**
     * 加载模板和页面输出 可以返回输出内容
     * @access public
     * @param string $templateFile 模板文件名
     * @param string $charset 模板输出字符集
     * @param string $contentType 输出类型
     * @return mixed
     */
    public function display($templateFile='',$charset='',$contentType='',$strShow=false) {
        G('viewStartTime');
        /// 视图开始标签
        tag('view_begin',$templateFile);
        /// 解析并获取模板内容
        $content = $this->fetch($templateFile); 
        if ($strShow==true){
        	/// 输出模板内容
	        return $this->show($content,$charset,$contentType,$strShow);
        }else{
        	/// 输出模板内容
	        $this->show($content,$charset,$contentType,$strShow);
	        /// 视图结束标签
	        tag('view_end');
        }
        
    }

    /**
     * 输出内容文本可以包括Html
     * @access public
     * @param string $content 输出内容
     * @param string $charset 模板输出字符集
     * @param string $contentType 输出类型
     * @param string $strShow 是否返信息
     * @return mixed
     */
    public function show($content,$charset='',$contentType='',$strShow=false){
        if(empty($charset))  $charset = C('DEFAULT_CHARSET');
        if(empty($contentType)) $contentType = C('TMPL_CONTENT_TYPE');
        /// 网页字符编码
        header('Content-Type:'.$contentType.'; charset='.$charset);
        header('Cache-control: private');  ///支持页面回跳
        header('X-Powered-By:ThinkPHP');
        if ($strShow==true){
        	/// 返回模板文件
        	return $content;
        }else{
        	/// 输出模板文件
        	echo $content; 
        }
        
    }

    /**
     * 解析和获取模板内容 用于输出
     * @access public
     * @param string $templateFile 模板文件名
     * @return string
     */
    public function fetch($templateFile='') {
        /// 模板文件解析标签
        tag('view_template',$templateFile);
        /// 模板文件不存在直接返回
        $frame_templateFile = str_replace(APP_PATH,THINK_PATH,$templateFile);
        if(!is_file($templateFile) && !is_file($frame_templateFile)) return NULL;
        /// 页面缓存
        ob_start();
        ob_implicit_flush(0);
        
        if('php' == strtolower(C('TMPL_ENGINE_TYPE'))) { /// 使用PHP原生模板
            /// 模板阵列变量分解成为独立变量
            extract($this->tVar, EXTR_OVERWRITE);
            /// 直接载入PHP模板
            include $templateFile;
        }else{
            /// 视图解析标签
            $this->tVar   =  array_merge($this->tVar,array('lang'=>L()));
            $params = array('var'=>$this->tVar,'file'=>$templateFile);
            tag('view_parse',$params);
        }
        /// 获取并清空缓存
        $content = ob_get_clean();
        /// 内容过滤标签
        tag('view_filter',$content);
        /// 输出模板文件
        return $content;
    }
}